package com.alibaba.manager;

import com.alibaba.common.SystemConstants;
import com.alibaba.define.domain.CommentDO;
import com.alibaba.mapper.CommentMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author : hanyang
 * @date : 2023-11-04 16:06
 **/

@Component
public class CommentManager {

    @Resource
    CommentMapper commentDAO;

    /**
     * 根据评论类型分页查询根评论
     */
    public List<CommentDO> selectRootCommentByArticleId(Integer type, Long articleId, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("articleId", articleId);
        parameterMap.put("offset", (--pageNum) * pageSize);
        parameterMap.put("size", pageSize);
        parameterMap.put("delFlag", SystemConstants.UN_DELETED);
        parameterMap.put("rootId", SystemConstants.ROOT_COMMENT_ID);
        parameterMap.put("type", type);
        return commentDAO.select(parameterMap);
    }


    /**
     * 根据评论类型查询 一个文章/link的所有子评论 不分页
     */
    public List<CommentDO> selectChildrenCommentByRootId(Long rootCommentId, Integer type) {
        HashMap<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("rootId", rootCommentId);
        parameterMap.put("delFlag", SystemConstants.UN_DELETED);
        parameterMap.put("offset", SystemConstants.NO_PAGINATION);
        parameterMap.put("size", SystemConstants.NO_PAGINATION);
        parameterMap.put("type", type);
        return commentDAO.select(parameterMap);
    }

    /**
     * 新增评论
     */
    public Integer addComment(CommentDO commentDO) {
        return commentDAO.insert(commentDO);
    }

    /**
     * 根据条件查询一个文章根评论总数
     */
    public Integer queryCommentCountByCondition(Long articleId, Integer type) {
        HashMap<String, Object> parameterMap = new HashMap<>();
        parameterMap.put("articleId", articleId);
        parameterMap.put("delFlag", SystemConstants.UN_DELETED);
        parameterMap.put("rootId", SystemConstants.ROOT_COMMENT_ID);
        parameterMap.put("type", type);
        return commentDAO.count(parameterMap);
    }

    /**
     * 批量查询子评论
     */
    public List<CommentDO> batchQueryChildrenComments(Set<Long> rootIds, Integer commentType) {
        // 查询所有子评论
        return commentDAO.selectChildrenCommentByRootIds(rootIds, commentType);
    }

}
